找传奇、传世资源到传世资源站!

USB打印小票DEMO

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

USB打印小票DEMOfrom clipboard
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Drawing.Printing;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace LptPrint_test{ public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } private StringReader sr; public bool Print(string sb) { bool result = true; try { sr = new StringReader(sb.ToString()); PrintDocument pd = new PrintDocument(); pd.PrintController = new System.Drawing.Printing.StandardPrintController(); pd.DefaultPageSettings.Margins.Top = 2; pd.DefaultPageSettings.Margins.Left = 0; //pd.DefaultPageSettings.PaperSize.Width = 320; //pd.DefaultPageSettings.PaperSize.Height = 5150; pd.PrinterSettings.PrinterName = pd.DefaultPageSettings.PrinterSettings.PrinterName;//默认打印机 pd.PrintPage = new PrintPageEventHandler(this.pd_PrintPage); pd.Print(); } catch (Exception ex) { result = false; } finally { if (sr != null) sr.Close(); } return result; } private void pd_PrintPage(object sender, PrintPageEventArgs ev) { //注意:此方法不管是串口,并口,还是USB接口的打印机都可以调用,前提是一定要将打印机设为默认打印机。 Font printFont = new Font("Arial", 9);//打印字体 float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; String line = ""; linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); while (count < linesPerPage && ((line = sr.ReadLine()) != null)) { yPos = topMargin (count * printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count ; } // If more lines exist, print another page. if (line != null) ev.HasMorePages = true; else ev.HasMorePages = false; } private void button1_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); sb.Append(" 农资管理系统 \n"); sb.Append("*************************************\n"); sb.Append("商品:" DateTime.Now.ToString() "\n"); sb.Append("客户:" DateTime.Now.ToString() "\n"); sb.Append("数量:" DateTime.Now.ToString() "\n"); sb.Append("金额:" DateTime.Now.ToString() "\n"); sb.Append("应收:" DateTime.Now.ToString() "\n"); sb.Append("实收:" DateTime.Now.ToString() "\n"); sb.Append("时间:" DateTime.Now.ToString() "\n"); sb.Append("*************************************\n"); Print(sb.ToString()); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复